﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Services;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;  
/// <summary>
/// UpdataMybookdata 的摘要说明
/// </summary>
public class UpdataMybookdata
{
    static string[] each;
    public static string file;
    public string output = "";
    public static Label Label2;
    SQL sql;
	public UpdataMybookdata()
	{
        sql = new SQL();
	}

    public void getinfoFromDouban(string bookname)
    {

        string htmlcode = GetHTML("https://api.douban.com/v2/book/search?q="+bookname+"&count=1");
        //htmlcode = "[" + htmlcode + "]";
        Regex reg = new Regex("\"large.*?jpg");
       
        MatchCollection ms = reg.Matches(htmlcode);

        //string imgurl="";
        //foreach (Match m in ms)
        //    imgurl = m.ToString();
        //imgurl = getImageUrl(imgurl);
        //if (!imgurl.Equals(""))
        //{
        //    sql.setDataIntoTable("Book_info", "IMAGEURL", "", imgurl, "bookname", bookname);
        //}


        //解析json之后，不用爬虫了
        var data = ScriptDeserialize(htmlcode);
        string[,] info = new string[6, 2];
        //{ 
        //{ "ImageURL", "" + data.books[0].images.large + "" }, 
        //{ "ContentAbstract", "" + data.books[0].author[0] + "" }, 
        //{ "Writer", "" + data.books[0].author[0] + "" }, 
        //{ "Price", "" + data.books[0].price + "" }, 
        //{ "ISBN", "ISBN='" + data.books[0].isbn13 + "'" }, 
        //{ "MULU", "" + data.books[0].catalog + "" }, 
        //};
        info[0, 0] = "ImageURL";
        info[1, 0] = "ContentAbstract";
        info[2, 0] = "Writer";
        info[3, 0] = "Price";
        info[4, 0] = "ISBN";
        info[5, 0] = "MULU";

        info[0,1] = "" + data.books[0].images.large + "";
        info[1,1] = "" + data.books[0].author[0] + "";
        info[2,1] = "" + data.books[0].author[0] + "";
        info[3,1] = "" + data.books[0].price + "";
        info[4,1] = "" + data.books[0].isbn13 + "";
        info[5,1] = "" + data.books[0].catalog + "";

        if (data!=null)
        {
            //sql.updateBOOKinfo("update Book_info set ContentAbstract='" + data.books[0].summary + "' , Writer='" + data.books[0].author[0] + "' , Price='" + data.books[0].price + "' ,  ISBN='" + data.books[0].isbn13 + "' , ImageURL='" + data.books[0].images.large + "' ,MULU='" + data.books[0].catalog + "'  where bookname='" + bookname + "'");
            for (int i = 0; i < 7;i++ )
            {
                for (int j = 0; j < 2;j++ )
                {
                    try
                    {
                        sql.updateBOOKinfo("update Book_info set " + info[i, j] + "='" + info[i, j+1] + "' where bookname='" + bookname + "'");
                    }
                    catch (System.Exception ex)
                    {
                        continue;
                    }
                }
                
            }
           
          

            
        }

        //string atc = getAtr(htmlcode, "summary");
        //string Price=getAtr(htmlcode,"")
        //if (!atc.Equals(""))
        //{
        //    sql.setDataIntoTable("Book_info", "ContentAbstract", "", atc, "bookname", bookname);
        //}



    }

    public string GetHTML(string url)
    {
        WebClient web = new WebClient();
        byte[] buffer = web.DownloadData(url);
        return Encoding.UTF8.GetString(buffer);
    }

    protected string getImageUrl(string or)
    {
        String httpurl1 = "";
        string txt = or;

        string re1 = ".*?";	// Non-greedy match on filler
        string re2 = "((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s\"]*))";	// HTTP URL 1

        Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
        Match m = r.Match(txt);
        if (m.Success)
        {
            httpurl1 = m.Groups[1].ToString();
            Console.Write("(" + httpurl1.ToString() + ")" + "\n");
        }
        Console.ReadLine();
        return httpurl1.ToString();
    }





    protected string getAtr(string json,string tag)
    {
        var data = ScriptDeserialize(json);
        string res = data.books[0].summary;
        return res;
    }



    public static DOUBAN ScriptDeserialize(string strJson)
    {

        JavaScriptSerializer js = new JavaScriptSerializer();

        return js.Deserialize<DOUBAN>(strJson);

    }


}